<?php
/**
 * Created by PhpStorm.
 * User: 程序猴
 * Date: 2018/5/7
 *  Q Q: 505413
 * Time: 9:32
 */

namespace app\admin\controller;
use app\admin\controller\Base;
use think\Db;
class Nav extends Base
{
    //nav列表
    public function index(){
        $lst = Db::name('nav')->order("id desc")->select();
        $this->assign('lst',$lst);
        return $this->fetch();
    }
    //修改列表的开启状态
    public function status_edit()
    {
        $id = input('param.id');
        $status = Db::name('nav')->where('id',$id)->value('status');//判断当前状态情况
        if($status==1)
        {
            $flag = Db::name('nav')->where('id',$id)->setField(['status'=>0]);
            return json(['code' => 1, 'data' => $flag['data'], 'msg' => '已禁止']);
        }
        else
        {
            $flag = Db::name('nav')->where('id',$id)->setField(['status'=>1]);
            return json(['code' => 0, 'data' => $flag['data'], 'msg' => '已开启']);
        }

    }
    //添加导航
    public function navAdd()
    {
        if(request()->isAjax()){
            $param = input('post.');

            if(!empty($param['status'])){
                if($param['status'] == 'on') {
                    $param['status'] = '1';
                }
            }else{
                $param['status'] = '0';
            }
            if(!empty($param['yes'])){
                if($param['yes'] == 'on') {
                    $param['yes'] = '1';
                }
            }else{
                $param['yes'] = '0';
            }
            $res = Db::name('nav')->insert($param);
            if($res){
                return json(['code' => 1, 'url' => 'nav/index', 'msg' => '添加导航成功']);
            }else{
                return json(['code' => -1, 'url' => 'nav/index', 'msg' => '添加导航失败，请重试']);
            }
        }
        return $this->fetch();
    }
    //编辑导航
    public function navEdit()
    {
        $id = input('param.id');
        if(request()->isAjax()){
            $param = input('post.');
            if(!empty($param['status'])){
                if($param['status'] == 'on') {
                    $param['status'] = '1';
                }
            }else{
                $param['status'] = '0';
            }
            if(!empty($param['yes'])){
                if($param['yes'] == 'on') {
                    $param['yes'] = '1';
                }
            }else{
                $param['yes'] = '0';
            }

            $data = Db::name("nav")->where(array('id'=>$id))->update($param);
            if(false === $data){
                return json(['code' => -1, 'url' => 'nav/index', 'msg' => '修改导航失败']);
            }else{
                return json(['code' => 1, 'url' => 'nav/index', 'msg' => '修改导航成功']);
            }
        }
        $nav = Db::name('nav')->where(array('id'=>$id))->find();
        $this->assign('nav',$nav);
        return $this->fetch();
    }
    //删除导航
    public function navDel()
    {
        $id = input('id');
        $res = Db::name('nav')->where(array('id'=>$id))->delete();
        if($res){
            return json(['code' => 1, 'url' => 'reduction/index', 'msg' => '删除导航成功']);
        }else{
            return json(['code' => -1, 'url' => 'reduction/index', 'msg' => '删除导航，请重试']);
        }
    }
}